package com.lxg.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 陆小根
 * date: 2022/04/25
 * Description:
 */

@RestController
public class VerifyCodeController {

  private final Producer producer;

  @Autowired
  public VerifyCodeController(Producer producer) {
    this.producer = producer;
  }

  @GetMapping("/vc.jpg")
  public String getVerifyCode(HttpSession session) throws IOException {
    // 1.生成验证码
    String text = producer.createText();
    // 2.放入session redis 实现
    session.setAttribute("kaptcha", text);
    // 3.生成图片
    BufferedImage bi = producer.createImage(text);
    FastByteArrayOutputStream fos = new FastByteArrayOutputStream();
    ImageIO.write(bi, "jpg", fos);
    // 4.返回 base64
    return Base64Utils.encodeToString(fos.toByteArray());
  }

}
